<?php
require("libraries/student.php");
class About extends Student{
    public function __construct(){
        parent::__construct();
        $this->load->model("mabout");
        $this->load->model("mcatab");
        $this->load->library("string");
    }
    public function index(){
        $config['base_url'] = base_url().'admin/about/index/';
        $config['total_rows'] = $this->mabout->count_all();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $config['next_link'] = "Next";
        $config['prev_link'] = "Prev";
        $config['first_link'] = "First";
        $config['last_link'] = "Last";
        $this->load->library("pagination");
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['act'] = 0;
        $data['title']="title about";
        $data['data']['info']= $this->mabout->listall($config['per_page'],$start);
        $data['template'] = "about/list";
        $this->load->view("layout",$data);
    }
    public function add(){
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['data'] = "";
        $data['title'] = "title about";
        $data['template'] = "about/add";
        $data['act'] = 0;
        $data['data']['category'] = $this->mcatab->get_all();
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_news");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "name" => $this->input->post("name"),
                    "catab_id" => $this->input->post("catab_id"),
                    "position" => $this->input->post("position"),
                    "country" => $this->input->post("country"),
                    "description" => $this->input->post("description"),
                    "content" => $this->input->post("content"),
                    "status"=>1,
                    "created_time"=>time(),
                );
                if($_FILES['image']['name'] != NULL){
                    $config['upload_path'] = './uploads/about';
                    $config['allowed_types'] = 'gif|jpg|jpeg|png';
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("image")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title'] = "about";
                        $data['template'] = "about/add";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['image'] = $data['file_name'];
                        $this->createThumbnail("uploads/about",$db['image'],133,93);
                    }
                }

                $this->mabout->add($db);
                redirect(base_url().'admin/about/index');
                //redirect(base_url()."admin/about/index");
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function update(){
        $id = $this->uri->segment(4);
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['title'] = "title about";
        $data['template'] = "about/edit";
        $data['data'] = "";
        $data['act'] = 0;
        $data['data']['category'] = $this->mcatab->get_all();
        $data['get'] = $this->mabout->get_data($id);
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_news");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
                $db = array(
                    "name" => $this->input->post("name"),
                    "catab_id" => $this->input->post("catab_id"),
                    "position" => $this->input->post("position"),
                    "country" => $this->input->post("country"),
                    "description" => $this->input->post("description"),
                    "content" => $this->input->post("content"),
                    "modified_time"=>time(),
                );
                if($_FILES['image']['name'] != NULL){
                    $config['upload_path'] = './uploads/about';
                    $config['allowed_types'] = 'gif|jpg|jpeg|png';
                    $config['max_size']	= '2000';
                    $config['max_width']  = '1400';
                    $config['max_height']  = '1400';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("image")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title'] = "about";
                        $data['template'] = "about/add";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['image'] = $data['file_name'];
                        $this->createThumbnail("uploads/about",$db['image'],133,93);
                    }
                }
                $data['act'] = 5;
                $this->mabout->update($db,$id);
                redirect(base_url().'admin/about/index');
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function del(){
        $id = $this->uri->segment(4);
        $data = $this->mabout->get_data($id);
        @unlink("uploads/about/".$data['image']);
        @unlink("uploads/about/thumb/".$data['image']);
        $this->mabout->del($id);
        redirect(base_url().'admin/about/index');
    }
    public function active(){
        $id   	= $this->uri->segment(4);
        $status	= $this->uri->segment(5);
        $db = array(
            "status" => $status
        );
        $this->mabout->update_status($db,$id);
        redirect(base_url().'"admin/about/');
    }
    public function createThumbnail($parth,$fileName,$width,$height){
        $this->load->library('image_lib');
        //$this->load->helper('thumbnail_helper');
        $config['image_library'] = 'gd2';
        $config['source_image'] = $parth.'/'.$fileName;
        $config['new_image'] = $parth.'/thumb/'.$fileName;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['thumb_marker'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
}